package cn.jofei.common.monitor;

import cn.jofei.common.monitor.cmds.Commands;
import cn.jofei.common.monitor.cmds.ShellActuator;
import cn.jofei.common.monitor.interf.Monitor;
import cn.jofei.common.monitor.msg.DingMsg;
import cn.jofei.common.utils.Utils;
import lombok.Data;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ServerPortMonitor
 *
 * @author Jofei Bao
 * @version ngbao v1.0
 * @date 2022年10月25日 18:40
 */
public class DiskMonitor extends Monitor {
    private final String ip = Utils.isWin() ?
            ShellActuator.executeSimple(Commands.WIN_IP).split(":")[1].trim() :
            ShellActuator.executeSimple(Commands.SH_GET_IP);
    /**
     * IP
     */
    private List<PathPer> checkList;

    private final Map<String, Double> diskSize = new HashMap<>();

    /**
     * @param monitName 监控服务名称
     * @param checkList 磁盘检测列表
     */
    public DiskMonitor(String monitName, List<PathPer> checkList) {
        super.monitName = monitName;
        this.checkList = checkList;
    }

    @Override
    public DingMsg getMsg() {
        if (Utils.nonNull(checkList)) {
            // 磁盘信息
            if (Utils.isWin()) {
                String diskInfo = ShellActuator.executeSimple(Commands.WIN_DISK);
                for (String line : diskInfo.split("\n")) {
                    if (Utils.nonNull(line) && line.contains(":")) {
                        String[] split = line.split(" ");
                        if (split.length >= 4) {
                            List<String> sizeInfo = Arrays.stream(line.split(" ")).filter(s -> !"".equals(s.trim()))
                                    .collect(Collectors.toList());
                            Double per = 100 * (Double.parseDouble(sizeInfo.get(3)) - Double.parseDouble(sizeInfo.get(2))) / Double.parseDouble(sizeInfo.get(3));
                            diskSize.put(sizeInfo.get(0).toLowerCase(), per);
                        }
                    }
                }
            } else {
                String diskInfo = ShellActuator.executeSimple(Commands.SH_DISK_PER);
                for (String line : diskInfo.split("\n")) {
                    String[] split = line.split("\\%");
                    diskSize.put(split[1].trim().toLowerCase(), Double.parseDouble(split[0].trim()));
                }
            }
            String msg = "";
            for (PathPer pathPer : checkList) {
                Double diskPer = diskSize.get(pathPer.getPath());
                if (Utils.nonNull(diskPer) && diskPer >= pathPer.getDiskPer()) {
                    msg += "> 磁盘路径：" + pathPer.getPath() + " 当前使用占比：" + DECIMAL_FORMAT.format(diskPer) + "% 警戒值：" + pathPer.getDiskPer() + "\n";
                }
            }
            diskSize.clear();
            if (Utils.isNull(msg)) {
                if (!isOk()) {
                    setOk(true);
                    return DingMsg.markdown("监控信息")
                            .text("#### 服务器(" + monitName + ")[" + ip + "]磁盘已恢复\n" +
                                    "###### 恢复时间：" + TIME_FORMATTER.format(LocalDateTime.now().atOffset(ZoneOffset.UTC)) + "\n")
                            .build();
                }
            } else {
                setOk(false);
                return DingMsg.markdown("监控信息")
                        .text("### 磁盘报警\n" +
                                "#### 服务器(" + monitName + ")[" + ip + "]磁盘超出警戒值:\n" +
                                msg +
                                "##### 报警时间：" + TIME_FORMATTER.format(LocalDateTime.now().atOffset(ZoneOffset.UTC)) + "\n" +
                                "##### 友情连接 [服务器列表]() \n")
                        .build();
            }
        }
        return null;
    }


    @Data
    static class PathPer {
        String path;

        Double diskPer;
    }
}

